<?php

namespace App\HttpController;

use App\Service\CommunityService;
use App\Service\SensitiveService;
use App\Utility\Verification;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="龙庭社圈-社圈话题")
 */
class Community extends BaseController
{
    /**
     * @Api(name="获取主题", path="/community/getTopic", version="1.0.0")
     * @ApiDescription(value="获取主题")
     * @Method(allow={POST})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @ApiSuccess({"code": 200,"result": [{"id": 3,"name": "记一件小事","icon": "https://s4.ax1x.com/2022/03/03/bYsHaV.png","image": "https://s4.ax1x.com/2022/03/03/bYsHaV.png","description": "","is_deleted": 0,"create_time": 0,"update_time": 0},{"id": 2,"name": "随手拍","icon": "https://s4.ax1x.com/2022/03/03/bYsb5T.png","image": "https://s4.ax1x.com/2022/03/03/bYsb5T.png","description": "","is_deleted": 0,"create_time": 0,"update_time": 0},{"id": 1,"name": "和春天约会","icon": "https://s4.ax1x.com/2022/03/03/bJDhND.jpg","image": "https://s4.ax1x.com/2022/03/03/bJDhND.jpg","description": "","is_deleted": 0,"create_time": 0,"update_time": 0}],"msg": "获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getTopic($limit, $page): bool
    {
        $res = CommunityService::getInstance()->getTopic($limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取动态", path="/community/getDynamic", version="1.0.0")
     * @ApiDescription(value="获取动态")
     * @Method(allow={POST})
     * @Param(name="tid", defaultValue=99, description="话题", from={JSON})
     * @Param(name="sort", defaultValue=0, description="排序方式", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @ApiSuccess({"code":200,"result":[{"avatar":"https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqxTb5ian0mjnKvYQzDMnLPm5g5zo8ZNZapvg0UIZmrRlSlLQzquicSP4cmma0lUVKIbYpxOXfk0cTA/132","username":"ꪮriᧁiꪀꪖꪶ .","from":"龙庭社圈","theme":"","content":"今天天气好，晴朗","see":0,"like":0,"comment":0,"images":"['https://cdn.uviewui.com/uview/album/1.jpg','https://cdn.uviewui.com/uview/album/2.jpg','https://cdn.uviewui.com/uview/album/3.jpg','https://cdn.uviewui.com/uview/album/4.jpg','https://cdn.uviewui.com/uview/album/5.jpg','https://cdn.uviewui.com/uview/album/6.jpg','https://cdn.uviewui.com/uview/album/7.jpg','https://cdn.uviewui.com/uview/album/8.jpg','https://cdn.uviewui.com/uview/album/9.jpg','https://cdn.uviewui.com/uview/album/10.jpg',]","time":"14:12:48"}],"msg":"获取成功"})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDynamic($tid, $sort, $page, $limit): bool
    {
        $res = CommunityService::getInstance()->getDynamic($tid, $sort, $page, $limit);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取龙庭社圈动态", path="/community/getCommunity", version="1.0.0")
     * @ApiDescription(value="获取龙庭社圈动态")
     * @Method(allow={POST})
     * @Param(name="sort", defaultValue=0, description="排序方式", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getCommunity($sort, $page, $limit): bool
    {
        $res = CommunityService::getInstance()->getCommunity($sort, $page, $limit);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="发表动态")
     * @Api(name="发表动态", path="/community/release", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="topic", defaultValue="0", description="话题id", from={JSON})
     * @Param(name="from", description="来自", from={JSON})
     * @Param(name="content", description="文字心情", required="", from={JSON})
     * @Param(name="images", description="图片", required="", from={JSON})
     * @Param(name="location", description="所在位置", required="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function release($topic, $from, $content, $images, $location): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $res = CommunityService::getInstance()->release($this->uid, $topic, $from, $content, $images, $location);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的发布", path="/community/getMyRelease", version="1.0.0")
     * @ApiDescription(value="获取我的发布")
     * @Method(allow={POST})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyRelease($limit, $page): bool
    {
        $res = CommunityService::getInstance()->getMyRelease($this->uid, $limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="点赞动态")
     * @Api(name="点赞动态", path="/community/likeDynamic", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function likeDynamic($gid): bool
    {
        $res = CommunityService::getInstance()->likeDynamic($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="查看动态详情")
     * @Api(name="查看动态详情", path="/community/getDynamicDetail", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDynamicDetail($gid): bool
    {
        $res = CommunityService::getInstance()->getDynamicDetail($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="查看评论")
     * @Api(name="查看动态评论", path="/community/getDynamicComment", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getDynamicComment($gid): bool
    {
        $res = CommunityService::getInstance()->getDynamicComment($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="动态评论")
     * @Api(name="动态评论", path="/community/commentDynamic", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="id", description="评论id", defaultValue=0, from={JSON})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @Param(name="content", description="内容", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function commentDynamic($id, $gid, $content): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $res = CommunityService::getInstance()->commentDynamic($this->uid, $id, $gid, $content);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取新闻资讯", path="/community/getNews", version="1.0.0")
     * @ApiDescription(value="获取新闻资讯")
     * @Method(allow={POST})
     * @Param(name="type", description="类型", defaultValue="99", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getNews($type): bool
    {
        $res = CommunityService::getInstance()->getNews($this->uid, $type);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取新闻资讯详情", path="/community/getNewDetail", version="1.0.0")
     * @ApiDescription(value="获取新闻资讯详情")
     * @Method(allow={POST})
     * @Param(name="id", description="资讯ID", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getNewDetail($id): bool
    {
        $res = CommunityService::getInstance()->getNewDetail($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="删除龙庭社圈动态", path="/community/delDynamic", version="1.0.0")
     * @ApiDescription(value="删除龙庭社圈动态")
     * @Method(allow={POST})
     * @Param(name="id", description="动态ID", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function delDynamic($id): bool
    {
        $res = CommunityService::getInstance()->delDynamic($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="龙庭社圈我的评论", path="/community/getMyComment", version="1.0.0")
     * @ApiDescription(value="龙庭社圈我的评论")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyComment(): bool
    {
        $res = CommunityService::getInstance()->getMyComment($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="龙庭社圈我的点赞", path="/community/getMyLike", version="1.0.0")
     * @ApiDescription(value="龙庭社圈我的点赞")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyLike(): bool
    {
        $res = CommunityService::getInstance()->getMylike($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="转发龙庭社圈动态", path="/community/forwardDynamic", version="1.0.0")
     * @ApiDescription(value="转发龙庭社圈动态")
     * @Method(allow={POST})
     * @Param(name="id", description="动态ID", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function forwardDynamic($id): bool
    {
        $res = CommunityService::getInstance()->forwardDynamic($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="龙庭社圈我的转发", path="/community/getMyForward", version="1.0.0")
     * @ApiDescription(value="龙庭社圈我的转发")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getMyForward(): bool
    {
        $res = CommunityService::getInstance()->getMyForward($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="点赞新闻", path="/community/newsLike", version="1.0.0")
     * @ApiDescription(value="点赞新闻")
     * @Method(allow={POST})
     * @Param(name="gid", description="新闻ID", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function newsLike($gid): bool
    {
        $res = CommunityService::getInstance()->newsLike($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     *
     * @ApiDescription(value="新闻评论")
     * @Api(name="新闻评论", path="/community/newsComment", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="id", description="评论id", defaultValue=0, from={JSON})
     * @Param(name="gid", description="新闻id", required="", notEmpty="", from={JSON})
     * @Param(name="content", description="内容", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function newsComment($id, $gid, $content): bool
    {
        $data = $this->request()->getBody()->__toString();
        $check = SensitiveService::getInstance()->verify($data);
        if (!$check['result']) {
            return $this->writeJsonError($check['msg']);
        }
        $res = CommunityService::getInstance()->newsComment($this->uid, $id, $gid, $content);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="查看新闻评论")
     * @Api(name="查看新闻评论", path="/community/getNewsComment", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="gid", description="动态id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getNewsComment($gid): bool
    {
        $res = CommunityService::getInstance()->getNewsComment($this->uid, $gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="获取未读消息")
     * @Api(name="获取未读消息", path="/community/getUnRead", version="1.0.0")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getUnRead(): bool
    {
        $res = CommunityService::getInstance()->getUnRead($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="获取未读消息详情")
     * @Api(name="获取未读消息详情", path="/community/getUnReadDetail", version="1.0.0")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getUnReadDetail(): bool
    {
        $res = CommunityService::getInstance()->getUnReadDetail($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}